package com.example.hospital.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.hospital.dto.PaymentQuery;
import com.example.hospital.dto.Result;
import com.example.hospital.entity.Payment;
import com.example.hospital.service.PaymentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("payment")
@Tag(name = "Payment", description = "支付记录相关接口")
public class PaymentController {
    @Resource
    private PaymentService paymentService;

    /**
     * 新增支付记录
     *
     * @param payment
     * @return payment id or error
     */
    @Operation(
            summary = "新增支付记录",
            description = "创建一个新的支付记录",
            responses = {
                    @ApiResponse(responseCode = "200", description = "添加成功, 返回支付ID"),
                    @ApiResponse(responseCode = "400", description = "请求参数错误")
            }
    )
    @PostMapping
    public Result<Integer> addPayment(@RequestBody Payment payment) {
        boolean saved = paymentService.save(payment);
        return saved ? Result.success("添加成功", payment.getPaymentId()) : Result.fail("添加失败");
    }

    /**
     * 根据id删除支付记录
     *
     * @param id
     * @return
     */
    @Operation(
            summary = "根据id删除支付记录",
            description = "通过支付记录ID删除指定的支付记录",
            responses = {
                    @ApiResponse(responseCode = "200", description = "删除成功"),
                    @ApiResponse(responseCode = "400", description = "支付记录未找到")
            }
    )
    @DeleteMapping("/{id}")
    public Result deletePayment(@PathVariable("id") Integer id) {
        boolean removed = paymentService.removeById(id);
        return removed ? Result.success("删除成功") : Result.fail("删除失败");
    }

    /**
     * 修改支付记录信息
     *
     * @param payment
     * @return
     */
    @Operation(
            summary = "修改支付记录信息",
            description = "通过传递新的支付记录信息更新现有支付记录",
            responses = {
                    @ApiResponse(responseCode = "200", description = "修改成功"),
                    @ApiResponse(responseCode = "400", description = "请求参数错误")
            }
    )
    @PutMapping
    public Result updatePayment(@RequestBody Payment payment) {
        boolean updated = paymentService.updateById(payment);
        return updated ? Result.success("修改成功") : Result.fail("修改失败");
    }

    /**
     * 根据id查询支付记录
     *
     * @param id
     * @return payment or error
     */
    @Operation(
            summary = "根据id查询支付记录",
            description = "根据支付记录ID查询支付记录信息",
            responses = {
                    @ApiResponse(responseCode = "200", description = "查询成功"),
                    @ApiResponse(responseCode = "400", description = "支付记录未找到")
            }
    )
    @GetMapping("/{id}")
    public Result<Payment> getPayment(@PathVariable("id") Integer id) {
        Payment payment = paymentService.getById(id);
        return payment != null ? Result.success("查询成功", payment) : Result.fail("不存在该支付记录");
    }

    /**
     * 查询所有支付记录
     *
     * @return payment list
     */
    @Operation(
            summary = "查询所有支付记录",
            description = "获取所有支付记录",
            responses = {
                    @ApiResponse(responseCode = "200", description = "查询成功"),
                    @ApiResponse(responseCode = "500", description = "服务器错误")
            }
    )
    @GetMapping
    public Result<List<Payment>> listPayments() {
        return Result.success(paymentService.list());
    }

    @Operation(
            summary = "根据患者ID查询支付记录",
            description = "根据患者ID查询支付记录",
            responses = {
                    @ApiResponse(responseCode = "200", description = "查询成功"),
                    @ApiResponse(responseCode = "400", description = "支付记录未找到")
            }
    )
    @GetMapping("/patient/{id}")
    public Result<List<Payment>> listPaymentsByPatientId(@PathVariable("id") Integer id) {
        List<Payment> payments = paymentService.lambdaQuery().eq(Payment::getPatientId, id).list();
        return payments != null ? Result.success("查询成功", payments) : Result.fail("不存在该患者的支付记录");
    }

    /**
     * 根据条件查询支付记录
     *
     * @param paymentQuery
     * @return payment list
     */
    @Operation(
            summary = "根据条件查询支付记录",
            description = "根据条件查询支付记录",
            responses = {
                    @ApiResponse(responseCode = "200", description = "查询成功"),
                    @ApiResponse(responseCode = "400", description = "支付记录未找到")
            }
    )
    @PostMapping("/query")
    public Result<List<Payment>> listPaymentsByQuery(@RequestBody PaymentQuery paymentQuery) {
        List<Payment> payments = paymentService.listPayments(paymentQuery);
        return Result.success("查询成功", payments);
    }
}
